/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Team. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/docs
 */
var apprain_a_s ={
	action				: base,
	form				: '#auto_chk_submit',
	debug				: false,
	res_flag			: '',
	res_msg				: '',
	auto_hide			: true,
	message_holder		: '.erros_message',
	loading_img			: base + '/images/loading.gif',
	show_debug			: function(response){
							if( apprain_a_s.debug)
							{
								alert( response );
							}
						},
	s_ready:			function ()
						{
							return (!apprain_f_v.error);
						},
	process_on_success: function()
						{
							$$(apprain_a_s.message_holder).set('html',apprain_a_s.res_msg);
							if( apprain_a_s.auto_hide )
							{
								$$(apprain_a_s.form).fade('out');
							}
						},
	process_on_error	: function()
						{
							$$(apprain_a_s.message_holder).set('html',apprain_a_s.res_msg);
						},
						loading_image: function(flag )
						{
							if( flag == 'show')
							{
								$$(apprain_a_s.message_holder).set('html','&nbsp;');
								$$(apprain_a_s.message_holder).setStyle("display","block");
								$$(apprain_a_s.message_holder).setStyle('background','url(' + apprain_a_s.loading_img + ') no-repeat');
							}
							else
							{
								$$(apprain_a_s.message_holder).setStyle('background','none');
							}
						},
						ajax_submit: function (){

							apprain_a_s.loading_image('show');
							$$(apprain_a_s.form).set('send', {
								method: 'post',
								onSuccess: function(responseTxt, responseXML)
								{
									apprain_a_s.loading_image('hide');
									apprain_a_s.show_debug(responseTxt);

									responseObj = JSON.decode(responseTxt);
									apprain_a_s.res_flag = responseObj._status;
									apprain_a_s.res_msg = responseObj._message;

									if( apprain_a_s.res_flag.toLocaleLowerCase() == 'success')
									{
										apprain_a_s.process_on_success();
									}
									else if( apprain_a_s.res_flag.toLocaleLowerCase() == 'redirect')
									{

										window.location = responseObj._location;
									}
									else
									{
										apprain_a_s.process_on_error();
									}
								},
								onFailure: function(headerName, value)
								{
								  alert('fail')
								}
							});
							$$(apprain_a_s.form).send(apprain_a_s.action);
						},
	init				: function(e){
							try
							{
								apprain_a_s.form = as_form_name;
								apprain_a_s.debug = as_debug;
								apprain_a_s.message_holder = as_message_element;
								apprain_a_s.auto_hide = as_auto_hide;
								apprain_a_s.loading_img = loading_img;
							}
							catch (err){}
							$$(apprain_a_s.message_holder).set('html','&nbsp;');
							apprain_a_s.loading_image('hide');
							apprain_a_s.action = $$(apprain_a_s.form).get('action');
						}
}